//
//  SPCTabBarController.m
//  SmallPigCar
//
//  Created by 张令林 on 2016/11/1.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "SPCTabBarController.h"
#import "SPCNavigationController.h"

@interface SPCTabBarController ()

@end

@implementation SPCTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加子视图
    [self addChildViewControllers];
}

#pragma mark - 创建windows的根控制器
- (void)addChildViewControllers {
    //创建字典,一次存储需要创建的tabBar内的元素
    NSArray *array = @[
                       @{@"clsName": @"SPCHomeViewController", @"title": @"首页", @"imageName": @"esc_iHome",@"imageNameSelect":@"esc_iHomeSel"},
                       @{@"clsName": @"SPCBuyCarViewController", @"title": @"买车",@"imageName": @"esc_iBuyCar",@"imageNameSelect":@"esc_iBuyCarSel"},
                       @{@"clsName": @"SPCSellCarViewController", @"title": @"卖车",@"imageName": @"esc_iSellCar",@"imageNameSelect":@"esc_iSellCarSel"},
                       @{@"clsName": @"SPCMineViewController", @"title": @"我的",@"imageName": @"esc_iMy",@"imageNameSelect":@"esc_iMySel"},
                       ];
    // 遍历数组,添加子控制器
    for (NSDictionary *dict in array)
    {
        // 通过字典创建对应的控制器
        UIViewController *vc = [self viewControllerWithDict:dict];
        //将控制器添加到子控制器中
        [self addChildViewController:vc];
    }
    
    //设置背景图片
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"toolbar_bottom_bar"]];
}
#pragma mark - 跟剧字典中的元素依次创建控制器
- (UIViewController *)viewControllerWithDict:(NSDictionary *)dict {
    // 1. 初始化控制器
    UIViewController *vc = [NSClassFromString(dict[@"clsName"]) new];
    // 2. 设置标题 = 导航栏标题+tabbar的标题
    vc.title = dict[@"title"];
    vc.navigationItem.title = dict[@"title"];
    //设置tabBar选中时的颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    // 3. 设置不同状态下显示的图片
    vc.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@", dict[@"imageName"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@", dict[@"imageNameSelect"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置选中后的颜色
    //CGSize imageSize = CGSizeMake(self.tabBar.bounds.size.width*0.3333, self.tabBar.bounds.size.height);
    //self.tabBar.selectionIndicatorImage = [self drawImage:imageSize];
    //嵌套一层UINavigationController
    SPCNavigationController *navController = [[SPCNavigationController alloc] initWithRootViewController:vc];
    //设置标题的颜色及大小
    [navController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:18]}];
    // 返回导航控制器
    
    return navController;
}
@end
